package com.abc.chatprj;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/chat/msgs")
public class MessagesServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        Object obj = this.getServletContext().getAttribute("msgs");
        out.print("<html><meta http-equiv='refresh' content='1'>");//自动刷新
        out.print("<body>");
        if(obj != null){
            List<String> list = (List<String>)obj;
            for(String m : list){
                out.print(m + "<br>");
            }
        }
        out.print("</body></html>");
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
